﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL;


namespace DrugStorehouseMIS.SystemProtected
{
    public partial class add_drug : Form
    {
        public add_drug()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DrugDAL drug = new DrugDAL();
            try
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {

                    drug.Insert(
                                dataGridView1.Rows[i].Cells[0].Value.ToString(),
                                dataGridView1.Rows[i].Cells[1].Value.ToString(),
                                dataGridView1.Rows[i].Cells[2].Value.ToString(),
                                dataGridView1.Rows[i].Cells[3].Value.ToString(),
                                dataGridView1.Rows[i].Cells[4].Value.ToString(),
                                dataGridView1.Rows[i].Cells[5].Value.ToString(),
                                dataGridView1.Rows[i].Cells[6].Value.ToString(),
                                dataGridView1.Rows[i].Cells[7].Value.ToString(),
                                Convert.ToInt32(dataGridView1.Rows[i].Cells[8].Value.ToString()),
                                Convert.ToInt32(dataGridView1.Rows[i].Cells[9].Value.ToString()),
                                Convert.ToSingle  ( dataGridView1 .Rows [i].Cells [10].Value .ToString ()),
                                Convert .ToSingle  ( dataGridView1 .Rows [i].Cells [11].Value .ToString ())
                              );
                }
                MessageBox.Show("添加成功");
            }
            catch (FormatException d) 
            {
                MessageBox.Show(d.Message); 
            }            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text.Trim() != "" && textBox2.Text.Trim() != "" && textBox3.Text.Trim() != "" &&
                 textBox4.Text.Trim() != "" && textBox5.Text.Trim() != "" && textBox6.Text.Trim() != "" && comboBox2.Text.Trim() != "" &&
                 textBox8.Text.Trim() != "" && textBox9.Text.Trim() != "" && textBox10.Text.Trim() != "" && textBox1.Text.Trim() != "" && textBox7.Text.Trim() != "")
            {
                object[] row = new object[]   { comboBox1.Text,textBox2 .Text,textBox3 .Text,textBox4 .Text,
                                                textBox5 .Text,textBox6 .Text, comboBox2.Text,
                                                textBox8 .Text,textBox9 .Text,textBox10.Text,textBox1 .Text ,textBox10 .Text 
                                               };

                dataGridView1.Rows.Add(row);
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
                comboBox2.Text = "";
                textBox8.Text = "";
                textBox9.Text = "";
                comboBox1.Text = "";
                textBox10.Text = "";
                textBox1.Text = "";
            }
            else
            {
                MessageBox.Show("请填写完整");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
            }
            dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 绑定药品类型
        /// </summary>
        /// <param name="combox"></param>
        public void BindDrugType(ComboBox combox)
        {
            DrugType drugtype = new DrugType();
            IList<DrugType> list = drugtype.GetAllDrugType();
            foreach (DrugType obj in list)
            {   
                combox.Items .Add (obj.DrugTypeName);                
            }            
        }

        /// <summary>
        /// 绑定包装单位
        /// </summary>
        public void BindMeasuringUnit()
        {
            MeasuringUnit measuringUnitBLL = new MeasuringUnit();
            IList<MeasuringUnit> list = measuringUnitBLL.GetAllMeasuringUnit();
            foreach (MeasuringUnit obj in list)
            {
                comboBox2.Items.Add(obj.MeasuringUnitName);
            }           
        }

        private void add_drug_Load(object sender, EventArgs e)
        {
            this.BindDrugType(comboBox1 );
            this.BindMeasuringUnit();
        }
    }
}